package com.ybkj.daijia.server.event.listener.freight;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.freight.FreightOrderAcceptEvent;
import com.ybkj.daijia.server.freight.Order;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverConsumedRecordService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.FinancialRecordService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class FreightOrderAcceptEventListener implements
    ApplicationListener<FreightOrderAcceptEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(FreightOrderAcceptEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;
    @Autowired
    private SmsService smsService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private DriverConsumedRecordService driverConsumedRecordService;

    @Autowired
    private FinancialRecordService financialRecordService;

    @Autowired
    private FreightService freightService;


    @Override
    @Async
    public void onApplicationEvent(FreightOrderAcceptEvent event) {

        logger.debug("order Accept event:{}", "");

        Order order = (Order) event.getSource();

        Driver driver = event.getDriver();

        Company orderCompany = companyService.findOne(order.getCompanyId());
        Company finalCompany = orderCompany;
        Company main = companyService.getMain();
        Setting setting = settingUtils.get();
        if (!main.getId().equals(orderCompany.getId()) && setting.isSmsMainCompany()) {
            finalCompany = main;
        }
        freightService.acceptInfoFee(driver, order);

        //发送短信
        SmsTemplet templet = smsService
            .findByAlias(finalCompany.getId(), SmsType.acceptFreightOrder);
        if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
            try {
                smsService.sendSms(order.getPassengerPhone(), "acceptFreightOrder",
                    "{\"服务人员名\":\"" + driver.getName() + "\",\"服务人员工号\":\"" + driver.getUsername()
                        + "\",\"服务人员电话\":\"" + driver.getPhone() + "\",\"订单号\":\"" + "***" + order
                        .getOrderNo().substring(8) + "\"}", finalCompany.getId(), 0,
                    finalCompany.getSignName());
            } catch (Exception e) {
                logger.error("发送短信发生异常：{}", e.getMessage());
            }
        }

        Passenger passenger = memberService.findOnePassenger(order.getPassengerId());
        passenger.setCid(memberService.findCid(passenger.getId()));
        //给客户推送
        if (passenger != null && StringUtils.isNotBlank(passenger.getCid())) {
            try {
                //发送设备推送
                PushData pushData = new PushData();
                pushData.setType("ORDER_ACCEPT_FREIGHT");
                pushData.setTitle("新提醒");
                pushData.setDescription("服务人员已接受您的订单");
                pushData.setData(order.getId());
                GeTuiMember.push(passenger, pushData, settingUtils.get());
            } catch (IOException e) {
                logger.error("push error : " + e.getMessage());
            }
        }

    }

}
